package com.lishuang.org.service.impl;

import com.lishuang.basic.service.impl.BaseServiceImpl;
import com.lishuang.basic.util.MD5Utils;
import com.lishuang.basic.util.StrUtils;
import com.lishuang.org.domain.Employee;
import com.lishuang.org.mapper.EmployeeMapper;
import com.lishuang.org.service.IEmployeeService;
import com.lishuang.user.domain.Logininfo;
import com.lishuang.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 重写save方法，关联logininfo表和员工-角色中间表
     * @param employee
     */
    @Override
    @Transactional
    public void save(Employee employee) {
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //密码加密
        String md5psw = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5psw);
        //复制employee对象字段内容到logininfo里面
        Logininfo logininfo = employee2Logininfo(employee);
        //先保存logininfo，返回自增长ID。
        logininfoMapper.save(logininfo);
        //设置employee的Logininfo_id
        employee.setLogininfo_id(logininfo.getId());

        super.save(employee);
        if (employee.getRole_id()!= null) {
            //添加员工-角色中间表数据
            employeeMapper.saveEmployeeRole(employee);
        }


    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        //管理员type默认为0
        logininfo.setType(0);
        return logininfo;
    }

    /**
     * 重写remove方法，关联logininfo表和员工-角色中间表
     * @param id
     */
    @Override
    @Transactional
    public void remove(Long id) {
        Employee employee = employeeMapper.loadById(id);
        //通过employee的get方法获取logininfo的id来同步删除数据
        logininfoMapper.remove(employee.getLogininfo_id());
        //删除员工-角色中间表数据
        employeeMapper.removeEmployeeRole(id);
        super.remove(id);
    }

    /**
     * 重写update方法，关联logininfo表和员工-角色中间表
     * @param employee
     */
    @Override
    @Transactional
    public void update(Employee employee) {
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //密码加密
        String md5psw = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5psw);
        //复制employee对象字段内容到logininfo里面
        Logininfo logininfo = employee2Logininfo(employee);
        //通过employee查询数据库获取logininfo_id
        Long logininfo_id = employeeMapper.loadById(employee.getId()).getLogininfo_id();
        //设置logininfo的logininfo_id
        logininfo.setId(logininfo_id);
        //同步修改logininfo
        logininfoMapper.update(logininfo);
        super.update(employee);

        //删除员工-角色中间表数据
        employeeMapper.removeEmployeeRole(employee.getId());

        if (employee.getRole_id()!= null) {
            //添加员工-角色中间表数据
            employeeMapper.saveEmployeeRole(employee);
        }
    }

    /**
     * 重写batchDelete方法，关联logininfo表和员工-角色中间表
     * @param ids
     */
    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            remove(id);
        }
    }

    /**
     * 根据店铺ID查询所有员工
     * @param shopId
     * @return
     */
    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.list(shopId);
    }
}
